//
//  SEGActivityDetailCommentCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/7.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityDetailCommentCell.h"

@interface SEGActivityDetailCommentCell ()

@property (nonatomic, strong) UIImageView* iconView; //头像
@property (nonatomic, strong) UILabel* nameLabel; //昵称
@property (nonatomic, strong) UILabel* addressLabel; //来自
@property (nonatomic, strong) UILabel* timeLabel; //时间
@property (nonatomic, strong) UILabel* contentLabel; //内容
@property (nonatomic, strong) SEGAdjustableButton* commentButton;
@property (nonatomic, strong) SEGAdjustableButton* praiseButton;
@property (nonatomic, strong) UIView* bottomLine;
@property (nonatomic, strong) UIView *imageContainer; //图片容器
/**
 当前cell的indexPath
 */
@property (nonatomic, strong) NSIndexPath *indexPath;

/**
 图片url数组
 */
@property (nonatomic, strong)NSMutableArray *imageUrlArray;

@end

@implementation SEGActivityDetailCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    // 图片
    self.iconView = [[UIImageView alloc] init];
    self.iconView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.width.height.mas_equalTo(38);
    }];
    [self.iconView seg_configWithRadius:2.0];
    // 名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = kColor_1;
    self.nameLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).offset(10);
        make.top.equalTo(self.iconView.mas_top).offset(2);
    }];
    
    //来自
    self.addressLabel = [[UILabel alloc] init];
    self.addressLabel.textColor = kColor_3;
    self.addressLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).offset(10);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(7);
    }];

    //内容
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.textColor = kColor_1;
    self.contentLabel.font = [UIFont systemFontOfSize:16];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.top.equalTo(self.iconView.mas_bottom).offset(15);
    }];
    
    self.imageContainer = [[UIView alloc] init];
    [self.contentView addSubview:self.imageContainer];
    [self.imageContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(0);
        make.top.equalTo(self.contentLabel.mas_bottom);
    }];
    
    
    //时间：时间标签位置是固定右下角的，评论和点赞位置会变
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.textColor = kColor_3;
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.right.equalTo(self.contentLabel);
        make.top.equalTo(self.imageContainer.mas_bottom).offset(15);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-15);
    }];
    

    //评论
    UIImage *image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_details_page_discuss")];
    SEGAdjustableButton *commentButton = [[SEGAdjustableButton alloc] init];
    [commentButton setTitle:@"评论" forState:UIControlStateNormal];
    [commentButton setTitleColor:kColor_3 forState:UIControlStateNormal];
    [commentButton setImage:image forState:UIControlStateNormal];
    commentButton.titleLabel.font = kFont_13;
    [commentButton addTarget:self action:@selector(seg_commentbuttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:commentButton];
    [commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.centerY.equalTo(self.timeLabel);
    }];
    [commentButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    commentButton.contentEdgeInsets = UIEdgeInsetsMake(5, 7, 5, 5);
    self.commentButton = commentButton;
    
    //点赞
    SEGAdjustableButton *praiseButton = [[SEGAdjustableButton alloc] init];
    [praiseButton setTitle:@"点赞" forState:UIControlStateNormal];
    [praiseButton setTitleColor:kColor_3 forState:UIControlStateNormal];
    [praiseButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_lifenews_like_small_nor")] forState:UIControlStateNormal];
    praiseButton.titleLabel.font = kFont_13;
    [praiseButton addTarget:self action:@selector(seg_praisebuttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:praiseButton];
    [praiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(commentButton.mas_right).offset(30);
        make.centerY.equalTo(commentButton);
    }];
    [praiseButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    praiseButton.contentEdgeInsets = UIEdgeInsetsMake(5, 7, 5, 5);//扩大点击区域，对应的间距也要改变（不调的时候左边图片有2的超出，可能和SEGAdjustableButton有关）
    self.praiseButton = praiseButton;
    
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
    }];
    self.bottomLine = line;
}

- (void)setUpCellWithCommentModel:(SEGActivityDetailCommentModel *)model indexPath:(NSIndexPath*)indexPath
{
    self.indexPath = indexPath;
    [self.iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, model.ico]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
    self.nameLabel.text = model.userName;
    self.addressLabel.text = [SEGUtils getShowAddressWithString:model.communityName];
    self.contentLabel.text = model.quizContent;
    self.timeLabel.text = [SEGUtils getShowDateWithTimer:model.createTime];
    
    [self seg_setCommentButtonWithCommentTotal:model.commentTotal];
    [self seg_setPraiseButtonWithPraiseTotal:model.praiseTotal isParise:model.isParise];
    
    [self setImagesWithQuizPic:model.quizPic];
    self.bottomLine.hidden = NO;
}

//邻居说详情页 邻居说详情
- (void)setUpCellWithCommentDetailModel:(SEGActivityDetailCommentDetailModel *)model indexPath:(NSIndexPath*)indexPath
{
    self.indexPath = indexPath;
    [self.iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, model.ico]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
    self.nameLabel.text = model.userName;
    self.addressLabel.text = [SEGUtils getShowAddressWithString:model.communityName];
    self.contentLabel.text = model.quizContent;
    
    self.timeLabel.text = [SEGUtils getShowDateWithTimer:model.createTime];
    
    self.commentButton.hidden = NO;
    [self seg_setCommentButtonWithCommentTotal:model.commentTotal];
    [self seg_setPraiseButtonWithPraiseTotal:model.praiseTotal isParise:model.isParise];
    [self.praiseButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.commentButton.mas_right).offset(30);
        make.centerY.equalTo(self.commentButton);
    }];
    
    [self setImagesWithQuizPic:model.quizPic];
    self.bottomLine.hidden = YES;
}

//邻居说详情页 评论的回复
- (void)setUpCellWithCommentAnswerModel:(SEGActivityDetailCommentAnswModel *)model indexPath:(NSIndexPath*)indexPath
{
    self.indexPath = indexPath;
    [self.iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, model.iconUrl]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
    self.nameLabel.text = model.answNick;
    self.addressLabel.text = [SEGUtils getShowAddressWithString:model.communityName];
    if (model.byReviewNick.length > 0) {
        self.contentLabel.text = [NSString stringWithFormat:@"回复 %@：%@", model.byReviewNick, model.answContent];
    }else {
        self.contentLabel.text = model.answContent;
    }
    
    self.timeLabel.text = [SEGUtils getShowDateWithTimer:model.createTime];
    
    self.commentButton.hidden = YES;
    [self seg_setPraiseButtonWithPraiseTotal:model.pariseTotal isParise:model.isPraise];
    [self.praiseButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.top.equalTo(self.nameLabel);
    }];
    
    [self setImagesWithQuizPic:nil];
    self.bottomLine.hidden = NO;
}

- (void)seg_setCommentButtonWithCommentTotal:(NSString *)commentTotal
{
    if ([commentTotal integerValue] <= 0) {
        [self.commentButton setTitle:@"评论" forState:UIControlStateNormal];
    }else {
        [self.commentButton setTitle:commentTotal forState:UIControlStateNormal];
    }
}

- (void)seg_setPraiseButtonWithPraiseTotal:(NSString *)praiseTotal isParise:(NSString *)isParise
{
    if ([praiseTotal integerValue] <= 0) {
        [self.praiseButton setTitle:@"赞" forState:UIControlStateNormal];
    }else {
        [self.praiseButton setTitle:praiseTotal forState:UIControlStateNormal];
    }
    
    if ([isParise isEqualToString:@"1"]) {
        [self.praiseButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_lifenews_like_small_pre")] forState:UIControlStateNormal];
        UIColor *pariseColor = kColor_T;
        [self.praiseButton setTitleColor:pariseColor forState:UIControlStateNormal];
    }else {
        [self.praiseButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_lifenews_like_small_nor")] forState:UIControlStateNormal];
        [self.praiseButton setTitleColor:kColor_3 forState:UIControlStateNormal];
    }
}

- (void)seg_commentbuttonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellCommentButtonActionWithIndexPath:)]) {
        [self.delegate cellCommentButtonActionWithIndexPath:self.indexPath];
    }
}

- (void)seg_praisebuttonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellPraiseButtonActionWithIndexPath:)]) {
        [self.delegate cellPraiseButtonActionWithIndexPath:self.indexPath];
    }
}

//设置评论图片
- (void)setImagesWithQuizPic:(NSString *)quizPic
{
    [self.imageContainer removeAllSubviews];
    
    if (!quizPic || quizPic.length == 0) {
        [self.imageContainer mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0);
        }];
        return;
    }
    
    CGFloat padding = 6.0;
    CGFloat imageWidth = floor((LYScreenWidth() - 30 - padding*2)/3.0);
    CGFloat topMargin = 9;
    NSArray *imageArray = [quizPic componentsSeparatedByString:@","];
    self.imageUrlArray = [NSMutableArray array];

    NSUInteger countInRow = imageArray.count == 4 ? 2 : 3; //一行最大图片个数
    [imageArray enumerateObjectsUsingBlock:^(NSString *imageString, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *imageUrlString =[NSString stringWithFormat:@"%@%@", kClientImageRootURL, imageString];
        [self.imageUrlArray addObject:imageUrlString];

        NSURL *url = [NSURL URLWithString:imageUrlString];
        UIImageView *imageView = [[UIImageView alloc] init];
        [imageView setImageWithURL:url placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")]];
        imageView.tag = 10;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;

        UIButton *imageButton = [[UIButton alloc] init];
        imageButton.tag = 100 + idx;
        [imageButton addTarget:self action:@selector(seg_imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.imageContainer addSubview:imageButton];
        [imageButton addSubview:imageView];

        CGFloat leftPadding = (idx % countInRow) * (imageWidth + padding);
        CGFloat topPadding = (idx / countInRow) * (imageWidth + padding) + topMargin;
        [imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(imageWidth);
            make.top.mas_equalTo(topPadding);
            make.left.mas_equalTo(leftPadding);
        }];

        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }];
    
    CGFloat imageContainerHeight = ceil(imageArray.count*1.0/countInRow)*(imageWidth + padding) + topMargin - padding;
    [self.imageContainer mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(imageContainerHeight);
    }];
}

- (void)seg_imageButtonPressed:(UIButton *)sender
{
    __block UIImageView *imageView;
    [sender.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.tag == 10) {
            imageView = (UIImageView *)obj;
        }
    }];
    if (self.delegate && [self.delegate respondsToSelector:@selector(cellImageTapWithImageUrlArray:SelectedImageView:SelectedIndex:)]) {
        [self.delegate cellImageTapWithImageUrlArray:self.imageUrlArray SelectedImageView:imageView SelectedIndex:sender.tag - 100];
    }
}

@end

